import { Bot_msg } from "../../model/bot/bot_msg";
import server from "../../model/client/server";
import func from "../../model/func";
import { mathType, packge } from "../../model/msgHandle/packge";

class index extends packge {

    constructor() {
        super()
    }
    init(): void {
        this.addMath(mathType.math_all, '阅读', ((msg: Bot_msg) => { this.readMail(msg) }), this,false)
        this.addMath(mathType.math_all, '查看邮件', ((msg: Bot_msg) => { this.readMail(msg) }), this,false)
        this.addMath(mathType.math_all, '邮件', ((msg: Bot_msg) => { this.sendMail(msg) }), this,false)
        this.addMath(mathType.math_start, '发送邮件', ((msg: Bot_msg) => { this.sendMail(msg) }), this,false)
        this.addMath(mathType.math_all, '我的ID', ((msg: Bot_msg) => { this.my_id(msg) }), this)
        this.addMath(mathType.math_start, '角色改名', ((msg: Bot_msg) => { this.body_rename(msg) }), this)
        this.addMath(mathType.math_start, '所有人', ((msg: Bot_msg) => { this.callAll(msg) }), this)
        this.addMath(mathType.math_start, '挂机', ((msg: Bot_msg) => { this.offline(msg) }), this)
        this.addMath(mathType.math_all, '停服', ((msg: Bot_msg) => { this.offServer(msg) }), this,false)
        this.addMath(mathType.math_all, '清空缓存', ((msg: Bot_msg) => { this.rmLoca(msg) }), this,false)
    }
    async rmLoca(msg:Bot_msg){

        let res =  await func.deleteFilesInDirectory('../../html/output_html')
        msg.repeText(`Successfully html\nsuccessCount:${res.successCount}\nFailed:${res.failCount}`)

        let res_public = await func.deleteFilesInDirectory('../../html/public')
        msg.repeText(`Successfully public\nsuccessCount:${res_public.successCount}\nFailed:${res_public.failCount}`)
    }
    offServer(msg:Bot_msg){
        server.api('Stop',{},msg)
    }
    offline(msg:Bot_msg){
        server.api('body/OffLine',{},msg)
    }
    callAll(msg:Bot_msg){
        if(msg.getContent(true).length > 240){
            msg.repeText('✨你要发送的消息过长啦~')
            return;
        }
        server.api('bag/function/CallAllMsg',{msg:msg.getContent(true).replace('所有人','')},msg);
        
    }
    body_rename(msg: Bot_msg) {
        if (msg.getContent().length == 0) {
            msg.repeText(`角色改名:角色改名 + 名称`)
            return
        }
        let regex = /^[\u4e00-\u9fa5A-Za-z0-9]+$/;
        // 测试输入的字符串是否符合规则
        let jude = regex.test(msg.getContent(true));
        if(!jude){
            msg.repeText(`角色名称只能为中文、数字、字母哦~`);
            return;
        }
        if(msg.getContent(true).length > 6){
            msg.repeText(`角色名称最多6个字噢~`);
            return;
        }
        server.api('body/Rename', { name: msg.getContent(true) }, msg)
    }
    my_id(msg: Bot_msg) {
        let onlyid = `qg_${msg.getAuthId()}`
        msg.repeText(`[ID编号]${onlyid}`)
    }
    sendMail(msg: Bot_msg) {
        let tpl = `发送邮件格式
你可以直接复制全部内容然后进行修改
标题:自定义邮件标题
内容:自定义邮件内容
收件人:收件人ID(对方发送[我的ID]可查看)
附件:背包ID，背包ID`
        if (msg.getContent().length == 0) {

            msg.repeText(tpl)
            return;
        }
        try {
            let str = msg.getContent(true).replace(/\r/g, '');
            // 提取标题、内容、收件人和附件信息
            let title = str.split("标题:")[1].split("内容:")[0];
            let content = str.split("内容:")[1].split("收件人:")[0];
            let recipient = str.split("收件人:")[1].split("附件:")[0];
            let attachments = str.split("附件:")[1].split(",");
            let attachments_num = [];
            for (let i = 0; i < attachments.length; i++) {
                const element = this.getNumber(attachments[i]);
                if (element == 0) {
                    msg.repeText(`背包附件ID${attachments[i]}不存在`)
                    break;
                }
                attachments_num.push(element)
            }
            server.api('body/mail/SendMail', {
                title: title,
                content: content,
                to_uuid: recipient,
                bag_idx: attachments_num
            }, msg)

        } catch (error) {
            msg.repeText(tpl)
        }

    }

    readMail(msg: Bot_msg) {
        server.api('body/mail/ReadMail', {}, msg)
    }

}
export default new index();